using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace NeedForSmash
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private Camera m_camera;
        public Camera Camera
        {
            get
            {
                return m_camera;
            }

            private set
            {
                m_camera = value;
            }
        }

        private Vector3 Position = Vector3.One;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.graphics.PreferredBackBufferWidth = 1440;
            this.graphics.PreferredBackBufferHeight = 900;

            graphics.PreferMultiSampling = true;

            this.IsFixedTimeStep = false;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Camera = new Camera(graphics.GraphicsDevice.Viewport);
            Battlefield bf = new FinalDestination(this);
            Components.Add(bf);
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        private void UpdateGamePad()
        {
            KeyboardState state = Keyboard.GetState(PlayerIndex.One);

            if (state.IsKeyDown(Keys.Escape))
            {
                Exit();
            }
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            UpdateGamePad();
            Camera.Update();

            base.Update(gameTime);
        }

        int elapsedTime = 0;
        int fps = 0;
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
#if DEBUG
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
            ++fps;
            if (elapsedTime >= 1000)
            {
                Console.WriteLine("Fps: " + fps);
                elapsedTime = 0;
                fps = 0;
            }
#endif

            GraphicsDevice.Clear(Color.CornflowerBlue);

            Camera.Update();

            base.Draw(gameTime);
        }
    }
}
